/**********************************************************************************
Name:  CaseCreatePageController
Copyright © 2012  Lodha Group
===================================================================================
===================================================================================
Purpose:
-------
Controller For Casecreatepage VF page for Customer Portal Users
===================================================================================
===================================================================================
History
-------
VERSION    AUTHOR                DATE             DETAIL              
1.0 -   CG Dev Team          20/05/2012      INITIAL DEVELOPMENT
**********************************************************************************/


public class CaseCreatePageController {

    public String caseid{get;set;}   
    public Case casecreate{get;set;}
    public Boolean Isbankdetail{get;set;}
    public Boolean Isinteractionpick{get;set;}
    public Boolean Isregistration{get;set;}
    public Boolean Issanctionpick{get;set;}
    public Boolean Ispdcompleted{get;set;}
    public Boolean Isdemandpick{get;set;} 
    public Boolean Isdisbursement{get;set;}
    public Boolean Isnotdisbursement{get;set;} 
    public String accountName {get; set;}
    public String accountId {get; set;}
    
     public Boolean render {get; set;}
     public Boolean showcolumn {get; set;}
     public Integer length {get;set;}
     public List<Account> accountfilterlist {get; set;}
     public List<Account> accounts{get; set;}
     public String Error {get; set;}
     public String query {get; set;}
     public String inputvalue {get; set;} 
     public List<accountwrapper> wrapperlist {get; set;}
    
    public CaseCreatePageController(ApexPages.StandardController controller) {
    
        casecreate=new Case();
        caseid=ApexPages.currentPage().getParameters().get('id');
         
       showcolumn=false;
    }

    
    public pageReference CreateCase(){
             system.debug(accountId);
        if(accountId!=null&&accountId!=''){
            system.debug(accountId);
            casecreate.AccountId=accountId;
            Insert casecreate;
            pageReference pageref=new pageReference('/apex/Casedetailpage?id='+casecreate.id);        
            return pageref;
        }else{
              ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'You must enter a value for account');
              ApexPages.addMessage(msg);   
              return null;   
        }
         
    }
    
    public void Onchange(){
    
        if(casecreate.Loan_Stages__c=='Lodha Login'||casecreate.Loan_Stages__c=='Bank Login'||casecreate.Loan_Stages__c=='Disbursement')
         {
            Isbankdetail=true;
            if(casecreate.Loan_Stages__c=='Disbursement'){
              Isdisbursement=true;
              Isnotdisbursement=false;
             }else{
               Isdisbursement=false;
               Isnotdisbursement=true;
             }
            
         }else{         
           Isbankdetail=false;
         }         
       if(casecreate.Loan_Stages__c=='Initial Call'||casecreate.Loan_Stages__c=='Appointment'||casecreate.Loan_Stages__c=='In Process')
         {
           Isinteractionpick=true;
               
         }else{
             Isinteractionpick=false;
         }
        if(casecreate.Loan_Stages__c=='Sanction')
           {
             
             Issanctionpick=true;
           }else{
               Issanctionpick=false;
           }
        if(casecreate.Loan_Stages__c=='Registration')
          {
           
            Isregistration=true;
          }else{
            Isregistration=false;
          }
        if(casecreate.Loan_Stages__c=='In Process')
          {
             Ispdcompleted=true;
          }else{
             Ispdcompleted=false;
          }
        if(casecreate.Loan_Stages__c=='Demand')
          {
              Isdemandpick=true;
          }else{
              Isdemandpick=false;
          }
    
    }
    
    public pageReference Cancel(){           
        pageReference pageref=new pageReference('/apex/CaseListView');        
        return pageref;
    }
 
 

  public class accountwrapper{    
        public String Accountid{get;set;}
        public String AccountName{get;set;}
    } 
 
     public PageReference runQuery()
        {
        
            String Querystring=null;

              String recordtype='Booked Account';
              if(inputvalue!=null && inputvalue!=''){
                inputvalue='%'+inputvalue+'%';                                
                     Querystring='Select Id,Name,FirstName,LastName from Account where (Firstname LIKE '+'\''+inputvalue+'\''+' OR LastName Like '+'\''+inputvalue+'\''+') AND(Account.RecordType.Name='+'\''+recordtype+'\''+') LIMIT 100';
                }else{
                       Querystring='Select Id,Name,FirstName,LastName from Account where Account.RecordType.Name='+'\''+recordtype+'\' LIMIT 100';
                    }         
                accountfilterlist=Database.Query(Querystring); 
                
             if(!accountfilterlist.isEmpty()){
                 showcolumn=true; 
                  wrapperlist=new List<accountwrapper>();
                  
                  for(Account acc: accountfilterlist)
                    {                            
                        accountwrapper wrapper=new accountwrapper();
                        wrapper.Accountid=acc.Id;
                        wrapper.AccountName=acc.Name;
                        wrapperlist.add(wrapper);            
                    }  
                 }          
                                                           
           //inputvalue=inputvalue.replace('%',''); 
           
           inputvalue=null;
            return null;
        }
    public PageReference ClearList(){
    
        wrapperlist=null;
        return null;
    }
    
    public static testmethod void testCaseCreatePageController () {
    
    
        
     
        Account a=new Account();
        a.RecordTypeId=[Select id from RecordType where SobjectType='Account' AND Name='Booked Account' Limit 1].Id;
        a.FirstName='Test';
        a.LastName='Account';
        insert a;
        
        ApexPages.StandardController controller = new ApexPages.StandardController(new Case());
        CaseCreatePageController casecreate0=new CaseCreatePageController(controller);
        
               
        casecreate0.casecreate=new Case();
        casecreate0.casecreate.Loan_Stages__c='Disbursement';
        casecreate0.runQuery();
        casecreate0.Onchange();
        casecreate0.accountId=(String)a.Id;
        casecreate0.CreateCase();       
        casecreate0.Cancel();
        casecreate0.ClearList();
        
        
        
       
        ApexPages.StandardController controller1 = new ApexPages.StandardController(new Case());        
        CaseCreatePageController casecreate1=new CaseCreatePageController(controller1);
        casecreate1.inputvalue='TestAccount';
        casecreate1.Onchange();
        casecreate1.runQuery();
    
    } 
    
}